package com.example.work.check;

import com.example.work.Excel.*;

import java.util.List;

/**
 * 数据比对演示程序
 * 展示完整的Excel导入和数据库比对流程
 */
public class DataComparisonDemo {
    
    public static void main(String[] args) {
        System.out.println("████████████████████████████████████████████████████████████");
        System.out.println("██                    资产数据导入和比对系统                    ██");
        System.out.println("██                     Asset Import & Comparison                     ██");
        System.out.println("████████████████████████████████████████████████████████████");
        
        try {
            // 步骤1: 初始化数据环境
            initializeDataEnvironment();
            
            // 步骤2: 演示完整的数据比对流程
            demonstrateFullComparisonFlow();
            
            // 步骤3: 演示高级功能
            demonstrateAdvancedFeatures();
            
            System.out.println("\n✅ 演示程序完成！");
            
        } catch (Exception e) {
            System.err.println("❌ 演示程序执行失败: " + e.getMessage());
            e.printStackTrace();
        }
    }
    
    /**
     * 初始化数据环境
     */
    private static void initializeDataEnvironment() {
        System.out.println("\n🔄 步骤1: 初始化数据环境");
        System.out.println("┌────────────────────────────────────────┐");
        
        // 初始化资产类型代码映射
        System.out.println("│ 初始化资产类型代码映射...                   │");
        AssetTypeCodeResolver.getCodeByTypeName("数据库"); // 触发初始化
        
        // 初始化模拟数据库
        System.out.println("│ 初始化模拟数据库服务...                   │");
        MockDatabaseService dbService = new MockDatabaseService();
//        dbService.printAllAssets();
        
        System.out.println("└────────────────────────────────────────┘");
        System.out.println("✅ 数据环境初始化完成");
    }
    
    /**
     * 演示完整的数据比对流程
     */
    private static void demonstrateFullComparisonFlow() {
        System.out.println("\n🔍 步骤2: 完整数据比对流程演示");
        System.out.println("┌────────────────────────────────────────────────────────────┐");
        
        // 模拟Excel数据导入
        System.out.println("│ 步骤2.1: 模拟Excel数据导入                              │");
        AssetImportService importService = new AssetImportService();
        simulateComprehensiveExcelData(importService);
        
        System.out.println("│ 步骤2.2: 执行数据比对分析                              │");
        ComparisonResult result = importService.compareWithDatabase();
        
        System.out.println("│ 步骤2.3: 生成详细比对报告                              │");
        if (result != null) {
            System.out.println("└────────────────────────────────────────────────────────────┘");
            System.out.println(result.generateDetailedReport());
            
            // 演示SQL生成
            if (!result.getInsertRecords().isEmpty()) {
                System.out.println("💾 生成批量插入SQL语句:");
                AssetComparisonService comparisonService = new AssetComparisonService();
                List<String> sqlStatements = comparisonService.generateInsertSqlStatements(result);
                
                System.out.println("┌── SQL示例 (前3条) ────────────────────────────────────────────────────────────────────────────────────────────┐");
                for (int i = 0; i < Math.min(3, sqlStatements.size()); i++) {
                    System.out.println("│ " + (i + 1) + ". " + sqlStatements.get(i).substring(0, Math.min(120, sqlStatements.get(i).length())) + "...");
                }
                System.out.println("└────────────────────────────────────────────────────────────────────────────────────────────┘");
            }
        }
        
        System.out.println("✅ 数据比对流程演示完成");
    }
    
    /**
     * 演示高级功能
     */
    private static void demonstrateAdvancedFeatures() {
        System.out.println("\n🎆 步骤3: 高级功能演示");
        System.out.println("┌────────────────────────────────────────┐");
        
        // 功能1: 自定义数据库数据比对
        System.out.println("│ 功能1: 自定义数据库数据比对              │");
        demonstrateCustomDatabaseComparison();
        
        // 功能2: 资产类型代码映射系统
        System.out.println("│ 功能2: 资产类型代码映射系统              │");
        demonstrateAssetTypeMapping();
        
        // 功能3: 数据校验和错误处理
        System.out.println("│ 功能3: 数据校验和错误处理              │");
        demonstrateDataValidation();
        
        System.out.println("└────────────────────────────────────────┘");
        System.out.println("✅ 高级功能演示完成");
    }
    
    /**
     * 模拟综合的Excel数据
     */
    private static void simulateComprehensiveExcelData(AssetImportService importService) {
        // 清空现有数据
        importService.clearAllLists();
        
        // 模拟各类型的资产数据
        
        // 1. 数据库资产（部分将匹配，部分为新增）
        importService.getDatabaseAssetList().add(createDatabaseAsset(
                "192.168.1.10", "主数据库服务器", "192.168.1.10", "3306")); // 将匹配
        importService.getDatabaseAssetList().add(createDatabaseAsset(
                "NEW-DB-100", "新数据库服务剂1", "10.10.10.100", "3306")); // 新增
        importService.getDatabaseAssetList().add(createDatabaseAsset(
                "NEW-DB-101", "新数据库服务剂2", "10.10.10.101", "5432")); // 新增
        
        // 2. 服务器资产
        importService.getServerAssetList().add(createServerAsset(
                "192.168.1.20", "Web服务器", "192.168.1.20", "22")); // 将匹配
        importService.getServerAssetList().add(createServerAsset(
                "NEW-SRV-200", "新服务划1", "10.10.10.200", "22")); // 新增
        
        // 3. Web资产
        importService.getWebAssetList().add(createWebAsset(
                "NEW-WEB-300", "新Web应用", "10.10.10.300", "8080")); // 新增
        
        // 4. 中间件资产
        importService.getMiddlewareAssetList().add(createMiddlewareAsset(
                "NEW-MW-400", "新中间件", "10.10.10.400", "9092")); // 新增
        
        // 5. 模拟一些无效数据（缺少必要字段）
        DatabaseAssetXlsx invalidAsset = new DatabaseAssetXlsx();
        invalidAsset.setAssetNumber("INVALID-001");
        invalidAsset.setAssetName("无效资产（缺少IP）");
        // 故意不设置IP和端口
        importService.getDatabaseAssetList().add(invalidAsset);
        
        System.out.println("模拟数据导入统计:");
        System.out.println("  数据库: " + importService.getDatabaseAssetList().size() + " 条");
        System.out.println("  服务器: " + importService.getServerAssetList().size() + " 条");
        System.out.println("  Web: " + importService.getWebAssetList().size() + " 条");
        System.out.println("  中间件: " + importService.getMiddlewareAssetList().size() + " 条");
    }
    
    private static void demonstrateCustomDatabaseComparison() {
        // 创建自定义数据库
        MockDatabaseService customDb = new MockDatabaseService();
        customDb.clearAllData();
        customDb.addTestDataForMatching();
        
        System.out.println("  自定义数据库中的数据:");
//        customDb.printAllAssets();
    }
    
    private static void demonstrateAssetTypeMapping() {
        System.out.println("  支持的资产类型映射(前10个):");

        String[] supportedTypes = AssetTypeCodeResolver.getSupportedAssetTypes().stream()
                .limit(10)
                .toArray(String[]::new);

        for (String typeName : supportedTypes) {
            String code = AssetTypeCodeResolver.getCodeByTypeName(typeName);
            System.out.println("    " + typeName + " → " + code);
        }
    }
    
    private static void demonstrateDataValidation() {
        System.out.println("  数据校验规则:");
        System.out.println("    ✓ 必须包含资产IP地址");
        System.out.println("    ✓ 必须包含端口信息");
        System.out.println("    ✓ 必须包含有效的资产类型代码");
        System.out.println("    ✓ 自动过滤空数据和无效记录");
    }
    
    // 创建模拟数据的辅助方法
    
    private static DatabaseAssetXlsx createDatabaseAsset(String assetNumber, String assetName, String ipAddress, String port) {
        DatabaseAssetXlsx asset = new DatabaseAssetXlsx();
        asset.setAssetNumber(assetNumber);
        asset.setAssetName(assetName);
        asset.setAssetIp(ipAddress);
        asset.setPort(port);
        asset.setDatabaseType("MySQL");
        asset.setDatabaseVersion("8.0");
        asset.setManufacturer("Oracle Corporation");
        asset.setBrandName("MySQL");
        return asset;
    }
    
    private static ServerAssetXlsx createServerAsset(String assetNumber, String assetName, String ipAddress, String port) {
        ServerAssetXlsx asset = new ServerAssetXlsx();
        asset.setAssetNumber(assetNumber);
        asset.setAssetName(assetName);
        asset.setAssetIp(ipAddress);
        asset.setPortList(port);
        asset.setOperatingSystemName("Linux");
        asset.setVersionNumber("CentOS 7.9");
        asset.setManufacturer("Dell");
        asset.setModel("PowerEdge R740");
        return asset;
    }
    
    private static WebAssetXlsx createWebAsset(String assetNumber, String assetName, String ipAddress, String port) {
        WebAssetXlsx asset = new WebAssetXlsx();
        asset.setAssetNumber(assetNumber);
        asset.setAssetName(assetName);
        asset.setAssetIp(ipAddress);
        asset.setPort(port);
        asset.setUrl("http://" + ipAddress + ":" + port);
        asset.setProtocol("HTTP");
        asset.setManufacturer("Apache Software Foundation");
        asset.setModel("Tomcat");
        asset.setVersionNumber("9.0");
        return asset;
    }
    
    private static MiddlewareXlsx createMiddlewareAsset(String assetNumber, String assetName, String ipAddress, String port) {
        MiddlewareXlsx asset = new MiddlewareXlsx();
        asset.setAssetNumber(assetNumber);
        asset.setAssetName(assetName);
        asset.setAssetIp(ipAddress);
        asset.setPort(port);
        asset.setMiddlewareType("Message Queue");
        asset.setMiddlewareVersion("2.8.0");
        asset.setManufacturer("Apache Software Foundation");
        asset.setBrandName("Kafka");
        return asset;
    }
}